package com.niguang.labour.admin.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.niguang.labour.admin.entity.AdminLaborUser;
import com.niguang.labour.admin.entity.WorkSiteUser;
import com.niguang.labour.admin.mapper.WorkSiteUserMapper;
import com.niguang.labour.admin.service.AdminLaborUserService;
import com.niguang.labour.admin.service.WorkSiteUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.niguang.labour.admin.vo.LaborListVo;
import com.niguang.labour.common.constant.ResultCode;
import com.niguang.labour.common.pojo.Result;
import com.niguang.labour.common.util.ApiTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 'work.v_personSite' is not BASE TABLE 服务实现类
 * </p>
 *
 * @author
 * @since 2018-06-10
 */
@Service
public class WorkSiteUserServiceImpl extends ServiceImpl<WorkSiteUserMapper, WorkSiteUser> implements WorkSiteUserService {

    @Autowired
    private ApiTokenUtils apiTokenUtils;

    @Autowired
    private AdminLaborUserService laborUserService;

    @Override
    public Result getLaborList(String keyword, String siteId, Integer type, Page<LaborListVo> page) {
        Long userId = apiTokenUtils.getUserId();
        if (userId == null) {
            return Result.build(ResultCode.UNAUTHORIZED, "用户未登录！");
        }
        AdminLaborUser adminLaborUser = laborUserService.selectById(userId);
        if (ObjectUtils.isEmpty(adminLaborUser)) {
            return Result.build(ResultCode.FORBIDDEN, "用户不存在！");
        }
        List<LaborListVo> laborList = baseMapper.getLaborList(keyword, siteId, type, adminLaborUser.getLaborCompanyId(), page);
        Map<String, Object> map = new HashMap<>(4);
        page.setRecords(laborList);
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        map.put("pages", page.getPages());
        map.put("current", page.getCurrent());
        return Result.build(ResultCode.SUCCESS, "班组列表查询成功！", map);
    }

    @Override
    public List<WorkSiteUser> getSiteUserByUserTeamId(Long userId, Long teamId) {
        return baseMapper.getSiteUserByUserTeamId(userId, teamId);
    }
}
